Click here to Skip to main content
15,915,328 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello all.
I've asking this, but I've try some solution and it still won't work :(
My problem is, when I generate a random, there's many same random value shown.
And I've improve my code for check them one by one, if the random value same, then the value have to randomed again, and it was not work :(

Here's my code :

VB
For i = 1 To PopuMain.members_count
            For j = 1 To PopuMain.GenoLength
                PopuMain.Members(i).Genome(j) = Rand_between(1, 513)
                If j > 1 Then
                    For k = 1 To j - 1
                        If PopuMain.Members(i).Genome(j) = PopuMain.Members(i).Genome(k) Then
                            PopuMain.Members(i).Genome(j) = Rand_between(1, 513)
                        End If
                    Next
                End If
            Next j


And this the random function :

VB
Public Function Rand_between(ByRef Minimum As Integer, ByRef Maximum As Integer) As Double
        Randomize()
        Rand_between = Int((Maximum - Minimum + 1) * Rnd() + Minimum)
    End Function


Please give me a solution(Check my code) for this one :(
Thanks a lot for your help :)


Sorry my english bad, I'm an indonesian :)
Posted
Updated 13-May-11 2:27am
v2

Createa HashSet, and like griff said, preload it with say, 10 random numbers. Since Hashsets don't allow duplicates, this will guarantee 10 unique random numbers.

Put your random number generator into a thread, so it can work behind the scenes (adding items to a hashset can negatively impact performance as the list gets longer and longer). If you establishg a threshhold where repeating randoms don't matter, you can trigger the hashset to be reduced by a given number of entries after adding a new batch of random values.

So, if you establish that numbers have to be different from the most previous 100 random values, you simply maintain a hashset collection that is never larger than 100 items.
 
Share this answer
 
Comments
Abdi tombang 13-May-11 8:52am    
Thanks John.
But, same with Griff, Whice part of my code should improved for apply this one??
Help me improve the wrong line from my code :(
OriginalGriff 13-May-11 8:53am    
Good idea!
ZeeroC00l 13-May-11 9:41am    
Good one. My +5 :)
Abdi tombang 13-May-11 10:19am    
Briliant idea john, thanks :)
But, would you like to tell me what should I'm improve from my code?
I'm really confuse to make your idea real (make it in my code)

would you like help me for this? :(
Create a class tha looks something like this:

C#
public class RandomValuesHash : HashSet<int>
{
    public int MaxCount { get; set; }
    public int MinValue { get; set; }
    public int MaxValue { get; set; }
    public int MinDelta { get; set; }

    private Random random = new Random(DateTime.Now.Milliseconds);

    public RandomValuesHash()
    {
        // reasonable defaults
        this.MaxCount = 100;
        this.MinValue = 1;
        this.MaxValue = 200;
        this.MinDelta = 90; // when the count drops to 90, top the list off
        // populate the collection
        AddValues();
    }

    public void AddValues()
    {
        if (this.Count <= this.MinDelta)
        {
            // now top-off the collection
            while (this.Count < MAX_COUNT)
            {
                this.Add(random.Next(this.MinValue, this.MaxValue));
            }
        }
    }

    public void RemoveItem(int index)
    {
        this.RemoveAt(index, 1);
    }

    public int GetNextValue()
    {
        int value = this[0];
        RemoveItem(0);
        AddValues();
        return value;
    }
}


The class above takes care of all the details, so all you have to do is instantiate when the app starts, and then start retrieving values from it. The list maintains itself.

C#
RandomValuesHash randomSet = new RandomValuesHash() { set your properties here if ou want something other than the defaults };

int newRandomValue = randomSet.GetNextValue();


To be brutally honest, when I gave you the description of what you needed to do, you shouldhave been able to do it (or figure it out on your own. Part of being a programmer is being able to analyze and design a solution as well as implement it. The implementation (writing the code) is the easy part, especially if the analysis and design have been done beforehand.

 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 13-May-11 14:03pm    
Right thing to do, my 5.
--SA
Abdi tombang 14-May-11 0:07am    
That's what I'm waiting for, thanks a lot john :)
Rule one: Don't do Randomize repeatedly: it encourages duplicates as it uses the system clock as a seed.

Call it once, outside all your loops and it should improve things.

The other thing is the nature of random numbers: they are random. That means that every number in the phase space has an equal chance of being the next number drawn: it does not rely (or even check) if a random number has been drawn already.

If you seriously want all different random numbers, then generate an array or a List of random numbers before you enter the loops, and remove all duplicates when you load it.
 
Share this answer
 
Comments
Abdi tombang 13-May-11 8:49am    
"If you seriously want all different random numbers, then generate an array or a List of random numbers before you enter the loops, and remove all duplicates when you load it."

Really good idea, I try for this one, but still can't make the code :(
Whice part of my code should improved for apply this one??
OriginalGriff 13-May-11 8:52am    
See JSOP's reply - It's a good idea!
Abdi tombang 13-May-11 8:56am    
I've his briliant idea.
But I'm still confuse how to implement his ide into my code :(
Whice part of my code should improved for apply this one??
Sergey Alexandrovich Kryukov 13-May-11 14:07pm    
All correct, but removing all duplicates would be really ugly. Duplicated should not be added, like it's done with hash sets and dictionaries. So, my 4 this time.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900